package com.yf.book.job;

import com.alibaba.fastjson.JSON;
import com.yf.book.job.dto.AddBookDTO;
import com.yf.book.modules.book.service.UserBookService;
import com.yf.job.service.JobService;
import lombok.extern.log4j.Log4j2;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 加入错题任务，用于在考试完成后，加入错题本
 * @author bool
 */
@Log4j2
@Component
public class UpdateBookJob implements Job {

    @Autowired
    private UserBookService userBookService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {


        JobDetail detail = jobExecutionContext.getJobDetail();
        String name = detail.getKey().getName();
        String group = detail.getKey().getGroup();
        String data = String.valueOf(detail.getJobDataMap().get(JobService.TASK_DATA));

        log.info("++++++++++定时任务：考完加入错题本");
        log.info("++++++++++jobName:{}", name);
        log.info("++++++++++jobGroup:{}", group);
        log.info("++++++++++taskData:{}", data);

        // 转换数据
        AddBookDTO dto = JSON.parseObject(data, AddBookDTO.class);

        // 修改可见状态
        userBookService.updateVisible(dto.getRefType(), dto.getRefId(), dto.getVisible());

    }



}
